<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:x="http://schemas.microsoft.com/xps/2005/06/resourcedictionary-key">

  <xsl:output method="xml" version="1.0" omit-xml-declaration="yes" indent="yes" encoding="utf-8"/>

  <xsl:include href="include/Slices.xslt"/>

  <xsl:variable name="ServerURL" select="/ResourceReportDataSet/General/ServerURL"/>

  <!-- Template para página HTML -->
  <xsl:decimal-format name="pt-br-Decimal" grouping-separator="." digit="#" zero-digit="0" decimal-separator="," /> 
  
  <xsl:template match="/">
    <FlowDocument ColumnWidth="800" TextAlignment="Center">

      <xsl:call-template name="StyleSettings"/>

      <Paragraph Style="{{StaticResource TitleParagraph}}">Team Hours Report</Paragraph>

      <xsl:variable name="funcDoMes">
        <xsl:for-each select="/ResourceReportDataSet/AssigneeEffortByMonth[TaskAssignee!=0]">
          <xsl:sort select="MonthWorkedHours" data-type="number" order="descending"/>
          <xsl:if test="position()=1">
            <xsl:value-of select="TaskAssignee"/>
          </xsl:if>
        </xsl:for-each>
      </xsl:variable>

      <xsl:if test="(/ResourceReportDataSet/GeneralRes/actualMonth &gt; /ResourceReportDataSet/AssigneeEffortByMonth/month) or (/ResourceReportDataSet/GeneralRes/actualYear &gt; /ResourceReportDataSet/AssigneeEffortByMonth/year)">
        <Table Style="{{StaticResource NoBorderTable}}">
          <TableRowGroup>
            <TableRow>
              <TableCell Style="{{StaticResource NoBorderCell}}">
                <Paragraph Style="{{StaticResource TitleParagraph}}">
                  Team Member of the Month &#160;
                  <xsl:value-of select="/ResourceReportDataSet/AssigneeEffortByMonth/month"/>/<xsl:value-of select="/ResourceReportDataSet/AssigneeEffortByMonth/year"/>
                  <LineBreak/><LineBreak/>
                  <Border Width="103" Height="103" BorderBrush="Black" CornerRadius="3,3,3,3" BorderThickness="1,1,1,1" >
                    <Image Source="{$ServerURL}/FactoryService/MemberImage.aspx?memberId={$funcDoMes}" Stretch="Fill" Width="100" Height="100"/>
                  </Border>
                  <LineBreak/>
                  <LineBreak/>
                  <Bold>
                    <xsl:value-of select="substring-after($funcDoMes,'\')"/>
                  </Bold>
                  was the team member who has really worked hard at
                  <Bold>
                    <xsl:value-of select="/ResourceReportDataSet/GeneralRes/monthName"/>.
                  </Bold>
                </Paragraph>
              </TableCell>
            </TableRow>            
          </TableRowGroup>
        </Table>
      </xsl:if>

      <Table Style="{{StaticResource NoBorderTable}}">
        <Table.Columns>
          <TableColumn Width="103"/>          
          <TableColumn Width="680"/>
        </Table.Columns>
        <TableRowGroup>
          <TableRow>
            <TableCell ColumnSpan="2" Style="{{StaticResource NoBorderCell}}">
              <Paragraph>
                A total of 
                <Bold>
                  <xsl:value-of select="format-number(sum(/ResourceReportDataSet/AssigneeEffortByMonth/MonthWorkedHours),'#.##0,00','pt-br-Decimal')"/>
                </Bold>
                hours was assigned this month.<LineBreak/>
              </Paragraph>
            </TableCell>
          </TableRow>

          <xsl:for-each select="/ResourceReportDataSet/AssigneeEffortByMonth[TaskAssignee!='0']">            
            <xsl:sort select="MonthWorkedHours" data-type="number" order="descending"/>
            <xsl:variable name="taskAssignee" select="TaskAssignee"/>
            <xsl:variable name="barLength">
              <xsl:choose>
                <xsl:when test="number(MonthWorkedHours)">
                  <xsl:choose>
                    <xsl:when test="MonthWorkedHours > 0">
                      <xsl:value-of select="MonthWorkedHours * 0.6"/>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="0"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="0"/>
                </xsl:otherwise>
              </xsl:choose>              
            </xsl:variable>
            <TableRow>
              <TableCell Style="{{StaticResource NoBorderCell}}">
                <Paragraph  Margin="0,0,0,10">
                  <Border Width="103" Height="103" BorderBrush="Black" CornerRadius="3,3,3,3" BorderThickness="1,1,1,1" >                    
                      <Image Source="{$ServerURL}/FactoryService/MemberImage.aspx?memberId={$taskAssignee}" Stretch="Fill" Width="100" Height="100"  />                    
                  </Border>
                  
                  <LineBreak/>
                  <xsl:value-of select="substring-after($taskAssignee,'\')"/>
                </Paragraph>
              </TableCell>              
              <TableCell Style="{{StaticResource NoBorderCell}}">
                <Table Style="{{StaticResource NoBorderTable}}">
                  <Table.Columns>
                    <TableColumn Width="{$barLength + 40}"/>
                  </Table.Columns>
                  <TableRowGroup>
                    <TableRow>
                      <TableCell Style="{{StaticResource NoBorderCell}}">
                        <Paragraph TextAlignment="Left" Margin="30,40,0,30">                        
                        <xsl:choose>
                          <xsl:when test="MonthWorkedHours>120">
                            <Border Width="{$barLength}" Height="20"
                              Background="LightGreen" BorderThickness="2,2,2,2" BorderBrush="DarkGreen" CornerRadius="3,3,3,3"/>
                          </xsl:when>
                          <xsl:otherwise>
                            <Border Width="{$barLength}" Height="20"
                              Background="Pink" BorderThickness="2,2,2,2" BorderBrush="Red" CornerRadius="3,3,3,3"/>
                          </xsl:otherwise>
                        </xsl:choose>
                        </Paragraph>
                      </TableCell>
                      <TableCell Style="{{StaticResource NoBorderCell}}">
                        <Paragraph TextAlignment="Left" Margin="10,40,0,30">
                          <xsl:value-of select="format-number(MonthWorkedHours,'#.##0,00','pt-br-Decimal')"/>
                        </Paragraph>
                      </TableCell>
                    </TableRow>
                  </TableRowGroup>
                </Table>
              </TableCell>
            </TableRow>
          </xsl:for-each>

          
        </TableRowGroup>
      </Table>

    </FlowDocument>

  </xsl:template>


  

</xsl:stylesheet>